import time

import facade
from xjlibrary.mprocesspoll.MThreadingRun import MThreadingRun
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -2)
sPath = BaseDir.get_new_path(TopPath, "download", "shusheng_imut", "download", "detail")
configfile = BaseDir.get_new_path(curPath, "db.ini")


class DownDetail(object):
    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(configfile, "db", logger=self.logger)
        self.proxy = {"http": "http://192.168.30.176:8062",
                      "https": "http://192.168.30.176:8062"}
        self.BASE_URL = 'http://202.207.22.16:9988'

    def select(self):
        sql = "select bookid,url from shusheng where stat=0 limit 10000"
        rows = self.mysqlutils.SelectFromDB(sql)
        return rows
        # for row in rows:
        #     self.down_detail(row)

    def down_detail(self, threadval, row):
        """
        下载详情页
        :param row:
        :return:
        """
        result_queue = threadval.result_queue
        bookid, baseurl = row
        dirpath = BaseDir.get_new_path(sPath, str(bookid)[:2])
        if not BaseDir.is_dir_exists(dirpath):
            BaseDir.create_dir(dirpath)
        filename = dirpath + "/" + str(bookid) + ".html"
        if BaseDir.is_file_exists(filename):
            self.logger.info("文件存在:{}".format(filename))
            sql = 'update shusheng set stat=1 where bookid={}'.format(bookid)
            result_queue.put(sql)
            return
        url = self.BASE_URL + "/" + baseurl
        feature = "图书详细信息"
        BoolResult, errString, r = facade.BaseRequest(url,
                                                      endstring="",
                                                      mark=feature,
                                                      proxies=self.proxy)
        if BoolResult:
            BaseDir.single_write_file(r.content.decode("GB18030"), filename, encoding="GB18030")
            self.logger.info("写入文件成功,第{}页".format(filename))
            sql = 'update shusheng set stat=1 where bookid={}'.format(bookid)
            result_queue.put(sql)
        else:
            self.logger.info("下载错误")
            sql = 'update shusheng set failcount=failcount+1 where bookid={}'.format(bookid)
            result_queue.put(sql)
        time.sleep(5)

    def InsertIntoDbFromList(self, ListSqls):
        self.mysqlutils.ExeSqlListToDB(ListSqls)


class DownListThreadRun(MThreadingRun):
    def __init__(self, num):
        super(DownListThreadRun, self).__init__(num)
        self.down = DownDetail()

    def getTask(self, *args, **kwargs):
        return self.down.select()

    def setTask(self, results=None, *args, **kwargs):
        for row in results:
            # 将每一页加入任务队列
            self.add_job(self.func, row)

    def dealresult(self, *args, **kwargs):
        self.down.InsertIntoDbFromList(self.results)

    def setProxy(self, proxysList=None):
        time.sleep(100)

    def is_break(self):
        return True

    def thread_pool_hook(self, thread_pool_dicts, thread, args, kwargs):
        return {}

    def fun(self, threadval, *args, **kwargs):
        self.down.down_detail(threadval, args[0])


if __name__ == "__main__":
    down = DownListThreadRun(5)
    down.run()
